<?php

class Quad_Form_Element_RadioGroup extends Quad_Form_Element_Select implements ArrayAccess, Iterator, Countable {

	public function __construct($label, $value='') {
		$this->setOptions(array());
		parent::__construct('', $label, $value);
		$this->addClassName('q-form-radio');
		$this->setLayout('br');
	}
	
	public function setLayout($class) {
		if (strpos($class,'_') === false) {
			$class = "Quad_Form_Element_GroupLayout_" . ucwords($class);
		}
		$this->_decorators[0] = $class;
		return $this;
	}
	
	public function getLayout() {
		return $this->_decorators[0];
	}
	
	public function getOpenTag() {
		return '';
	}
	
	public function getCloseTag() {
		return '';
	}
	
	public function setOptions(array $options) {
		unset($this->_options);
		$this->_options = new Quad_Form_Element_RadioCollection($this, $options);
		$this->_options->setValue($this->_value);
		$this->_options->invoke('setAttribute', array('name', $this->name));
		return $this;
	}
	
  // ArrayAccess Interface Functions
  public function offsetExists($offset) {
    return isset($this->_options[$offset]);
  }

  public function offsetGet($offset) {
    return $this->_options[$offset];
  }
 
  public function offsetSet($offset, $value) {
  	$this->_options[$offset] = $value;
  }
 
  public function offsetUnset($offset) {
  	unset($this->_options[$offset]);
  }

  // Countable Interface Functions
  public function count() {
    return count($this->_options);
  }
 
  // Iterator Interface Functions
  public function current() {
    return current($this->_options);
  }
 
  public function key() {
    return key($this->_options);
  }
 
  public function next() {
    return next($this->_options);
  }
 
  public function rewind() {
    return reset($this->_options);
  }
 
  public function valid() {
    return (key($this->_options) !== null);
  }
 
  public function getIterator() {
    return $this->_options;
  }
  
  // other helpful functions
  public function clear() {
  	$this->_options = array();
  	return $this;
  }

  public function toArray() {
  	return $this->_options;
  }

}
